/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi Distribution.

    Maxsi Distribution is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Distribution is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Distribution. If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiDistributionContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiDistributionContributors.h. For more information please see
	MaxsiDistributionContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]

	MaxsiDistribution.dll
	Distributing since 2008

	ReportCrashes.cpp
	Reporting Crashes to the proper developers.

****************************************************************************/


#if 0

	#include "MaxsiEngine.h"
	#include "MaxsiDistributionLink.h"
	#include <io.h>
	#include <direct.h>
	#include <iostream> 
	#include <fcntl.h>
	#include <SYS\Stat.h>
	#include <process.h> 
	#include "MaxsiUpdate.h"

	extern	UpdateProductArguments		g_Arg;
			List						CrashList	=	0;

	// Add the crash to the list of known crashes
	MISFUNC(Crash,MaxsiCrashUploader)
	{
		if ( GetListSize(In()) != 1 || !In[0]) { return 0; }

		if	(!CrashList)
		{
			CrashList		=		CreateList(1);
			SetListEntry(CrashList,0,In[0],strlen(In[0])+1);
		}
		else
		{
			SetListSize(CrashList,GetListSize(CrashList)+1);
			SetListEntry(CrashList,GetListSize(CrashList)-1,In[0],strlen(In[0])+1);
		}

		return 1;
	}

	// Scans the crashes and sees if they have anything to do with the given game
	// and if they do, and they are unknown, upload them!
	int		ScanCrashes(char* DumpFolder, char* GamePath )
	{
		return 1;
	}

	int		OutputCrashes(char* File)
	{
		int fh = 0;							
		_sopen_s(&fh,File, O_CREAT | O_TRUNC | O_BINARY | O_WRONLY, _SH_DENYNO, _S_IREAD | _S_IWRITE);

		if (fh == -1)
		{
			// dang
			return 0;
		}
		else
		{
			// Write a new script!

			char*	Header		=	
			"//*****************************************************************************\r\n"
			"//\r\n"
			"//	MaxsiDistributionCrashes.mis\r\n"
			"//	AUTOMATICALLY GENERATED. DO NOT EDIT.\r\n"
			"//\r\n"
			"//	Contains a list of 'known' crash logs that has been scanned\r\n"
			"//	and uploaded if related to the mod in question. Allows the game\r\n"
			"//	to detect new crashes only and avoid loading tons of crash reports.\r\n"
			"//\r\n"
			"//\r\n"
			"//*****************************************************************************\r\n"
			"\r\n"
			"Application/x-MaxsiEngineInstallerScript/2.0\r\n"
			"Application: x-MaxsiEngineMaxsiCrashUploader\r\n"
			"\r\n";

			_write(fh,Header,strlen(Header));

			size_t	N	=	GetListSize(CrashList);

			for (size_t I = 0; I < N; I++)
			{
				Header	=	(char*)GetListEntry(CrashList,(unsigned int)I);
				
				_write(fh,Header,strlen(Header));
				_write(fh,"\r\n",strlen("\r\n"));
			}

			_close(fh);
		}
		return 1;
	}

	// Returns true if the product can be run, false if an update-process has begun.
	LINK	int	ReportCrashes( UpdateProductArguments Arg )
	{
		if (sizeof(Arg) != Arg.Size) { return RESULT_INVALID_PARAMETER_SIZE; }
		g_Arg = Arg;

		// Find the file with all the crashes listed!
		// Convert from [Steam]/SteamApps/SourceMods/Altered Transmission/
		// to			[Steam]/SteamApps/SourceMods/Altered Transmission/bin/MaxsiDistributionCrashes.mis

		char*	CrashFile		=		BuildString(2,Arg.FolderName,"bin/MaxsiDistributionCrashes.mis");


		int fh = 0;							
		_sopen_s(&fh,CrashFile, O_BINARY | O_RDONLY, _SH_DENYNO, _S_IREAD | _S_IWRITE);

		if (fh == -1)
		{
			// dang
		}
		else
		{
			// Execute the script!

			long	MISSize		=	_filelength(fh);
			char*	MISData		=	new char[MISSize+1];

			_read(fh,MISData,MISSize);			
			
			MISData[MISSize]	=	0;

			_close(fh);

			MISRun(MISData,"MaxsiCrashUploader");
		}

		// Upload the unknown crashes
		ScanCrashes(0,0);

		// Write the file containing all the known crashes
		OutputCrashes(CrashFile);


		delete[] CrashFile;

		return RESULT_RUN;
	}
#endif